<template>
  <div style="display: inline-block;text-align: left;">
    <!-- 修改 - btn -->
    <el-button type="primary" icon="el-icon-edit" circle @click="dialogTableVisible = true"/>
    <el-dialog v-el-drag-dialog :visible.sync="dialogTableVisible" width="600px" title="修改" @close="close">
      <el-form ref="row_data" :model="row_data" :rules="rules" label-position="left" label-width="100px" style="width: 450px; margin-left:50px;">
        <el-form-item :label="'id'" prop="id">
          <el-input v-model="row_data.id" placeholder="id" disabled/>
        </el-form-item>
        <el-form-item :label="'账号id'" prop="account_id">
          <el-input v-model="row_data.account_id" placeholder="账号id" disabled/>
        </el-form-item>
        <el-form-item :label="'权限角色'" prop="shop_role_id">
          <el-select v-model="row_data.shop_role_id">
            <el-option v-for="op in select_role" :key="op.id" :label="op.name+'（'+op.role+'）'" :value="op.id"/>
          </el-select>
        </el-form-item>
        <el-form-item :label="'状态'" prop="status">
          <el-radio v-for="item in select_status" v-model="row_data.status" :key="item.value" :label="item.value">{{ item.label }}</el-radio>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm()">编辑</el-button>
          <el-button @click="close()">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import elDragDialog from '@/directive/el-dragDialog' // base on element-ui
import store from '@/store'
import { getRoleList, edit } from '@/api/user/admin_user'
import { client } from '@/utils/alioss'

export default {
  name: 'DragDialogDemo',
  directives: { elDragDialog },
  props: {
    row_data: {
      type: Object,
      default: function() {
        return null
      }
    }
  },
  data() {
    return {
      select_status: [
        { value: 0, label: '禁用' },
        { value: 1, label: '正常' }
      ],
      select_role: [],
      rules: {
        id: [
          { required: true, message: 'id不能为空', trigger: 'blur' }
        ],
        account_id: [
          { required: true, message: '账号id不能为空', trigger: 'blur' }
        ],
        shop_role_id: [
          { required: true, message: '登录名不能为空', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '状态不能为空', trigger: 'blur' }
        ]
      },
      dialogTableVisible: false
    }
  },
  created: function() {
    console.log(this.row_data)
    getRoleList(store.getters.token).then(res => {
      if (res.data.status) {
        this.$set(this, 'select_role', res.data.list.data)
      }
    })
  },
  methods: {
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isPNG = file.type === 'image/png'
      const isLt2M = file.size / 1024 / 1024 < 1
      if (!isJPG && !isPNG) {
        this.$message.error('上传图片只能是 JPG、PNG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传图片大小不能超过 1MB!')
      }
      return (isJPG | isPNG) && isLt2M
    },
    upload_head_img(file) {
      const that = this
      const fileName = 'user-admin_user-head_img' + file.file.uid
      client().put(fileName, file.file).then((res) => {
        this.$set(that.row_data, 'head_img', res.url)
        that.$message.success('图片上传成功！')
      }).catch((err) => {
        that.$message.error('图片上传失败，原因：' + err)
      })
    },
    submitForm() {
      this.$refs.row_data.validate(valid => {
        if (valid) {
          const data = this.row_data
          edit(store.getters.token, data).then(res => {
            console.log(res)
            if (res.data.status) {
              this.$message.success('修改成功！')
              this.close()
            } else {
              this.$message.error('修改失败！原因：' + res.data.msg)
            }
          })
        } else {
          return false
        }
      })
    },
    close() {
      this.$emit('getLists')
      this.dialogTableVisible = false
    }
  }
}
</script>
